#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/8/6 19:42
# @Author  : 李中华
# @Email   : 1260429766@qq.com
# @File    : paramsplit.py

class Paramsplit:
    def paramsplit(self,corr,results):
        keys = {}
        databack = results
        '''
        用例中关联结果拆分，将之前获取的results结果值，替换到关联参数corr中
        :param corr:
        :param results:
        :return:
        '''
        corr = str(corr).replace('\n','').replace('\r','').split(';')
        for cor in corr:
            if cor.startswith('$'):
                corKey = cor.split("=")[0][2:-1] #获取所有key值
                for tempKey in cor.split("=")[1][1:-1].split(']['):
                    if isinstance(results,list):
                        for result in results:
                            if tempKey in result.keys():
                                results = result.get(tempKey)
                    else:
                        pass
                keys[corKey] = results
                results = databack #恢复传过来的数据，供下一个key值使用
        return keys

if __name__ == '__main__':
    corr = '${memberId}=[list][gradeList][memberId];${mobile}=[list][mobile];${totalGainPoint}=[list][pointList][totalGainPoint]'
    results = [{'pageSize': 20, 'totals': 0, 'list': [
        {'id': 1068030368, 'cardPlanId': 320013, 'cardNumber': 'TAOBAO自动化数据', 'status': 'NORMAL', 'name': 'TAOBAO自动化数据',
         'created': '2020-11-10 10:55:01', 'openPlatCode': 'TAOBAO', 'modified': '2020-11-10 10:55:01',
         'mobile': '19909090909', 'mixMobile': '91cb95d490f5277997799f3bf5c7a642', 'openShopId': '102417112',
         'tenantId': 'qiushi6', 'mobileModified': '2020-11-10 10:55:01', 'platCode': 'TAOBAO', 'pointList': [
            {'id': 1067840366, 'tenantId': 'qiushi6', 'cardPointPlanId': 324405, 'currentValidPoint': 0,
             'currentFreezePoint': 0, 'totalGainPoint': 6, 'totalConsumePoint': 6, 'totalOverduePoint': 0,
             'modified': '2020-11-10 10:55:03'}], 'gradeList': [
            {'id': 1067940366, 'memberId': 1068030368, 'cardPlanId': 320013, 'tenantId': 'qiushi6',
             'cardGradePlanId': 324505, 'currentGradeId': 327538, 'currentEffectiveDate': '2020-01-23',
             'currentOverdueDate': '2025-12-12', 'modified': '2020-11-10 10:55:03'}]}], 'pageNum': 1}]
    print(Paramsplit().paramsplit(corr, results))
    # Paramsplit().paramsplit(corr, results)
    # k = {'pageSize': 20, 'totals': 0}
    # print(k.get("pageSize"))
